/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.service.material;

import java.util.ArrayList;
import java.util.List;

import com.server.common.utils.MD5Utils;
import com.server.common.utils.StringUtils;
import com.server.common.utils.UUIDUtils;
import com.server.modules.sys.vo.web.MaterialUploadWrap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.server.common.persistence.Page;
import com.server.common.service.CrudService;
import com.server.modules.sys.entity.material.MaterialUpload;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.dao.material.MaterialUploadDao;
import org.springframework.util.CollectionUtils;

/**
 * 资料上传管理Service
 * @author 王磊
 * @version 2016-08-22
 */
@Service
@Transactional(readOnly = true)
public class MaterialUploadService extends CrudService<MaterialUploadDao, MaterialUpload> {
	public MaterialUpload get(String id) {
		return super.get(id);
	}

	public List<MaterialUpload> findList(MaterialUpload materialUpload) {
		return super.findList(materialUpload);
	}

	public Page<MaterialUpload> findPage(Page<MaterialUpload> page, MaterialUpload materialUpload) {
		return super.findPage(page, materialUpload);
	}

	@Transactional(readOnly = false)
	public void save(MaterialUpload materialUpload) {
		super.save(materialUpload);
	}

	@Transactional(readOnly = false)
	public void delete(MaterialUpload materialUpload) {
		super.delete(materialUpload);
	}
	public MaterialUpload findMaterialUploadbymd5(String md5){
		MaterialUpload materialUpload = new MaterialUpload();
		materialUpload.setMd5(md5);
		List<MaterialUpload> list = this.findList(materialUpload);
		if (list != null &&list.size() > 0){
			MaterialUpload ret = list.get(0);
			return ret;
		}else {
			return null;
		}
	}
	@Transactional(readOnly = false)
	public boolean doCheck(MaterialUpload obj) {
		obj.preUpdate();
		return dao.update(obj)>0?true:false;
	}
	@Transactional(readOnly = false)
	public Object doCheckBatch(Member member, Boolean isClear) {
		MaterialUpload obj=new MaterialUpload();
		obj.preUpdate();
		obj.setMemberId(member.getId());
		obj.setIsClear(isClear);
		return dao.updateIsClearBatch(obj)>0?true:false;
	}
	@Transactional(readOnly = false)
    public boolean insertMemberMuByGroup(MaterialUploadWrap wrap, String memberId,String groupId,String isNew) throws Exception {
		List<MaterialUpload> mus = wrap.getMus();
		//编辑 有分组id
        if(StringUtils.isBlank(memberId)|| CollectionUtils.isEmpty(mus)){
            return false;
        }else if(StringUtils.isBlank(groupId)&&StringUtils.isBlank(isNew)){
            return false;
        }else if(StringUtils.isBlank(groupId)){
            groupId= UUIDUtils.getUUID();
        }
			List<MaterialUpload> validMus=new ArrayList<>();
			for (MaterialUpload mu : mus) {
				//必要参数校验
				String value = mu.getValue();
				String radiovalue = mu.getRadiovalue();
				String iconvalues = mu.getIconvalues();
				if(StringUtils.isNotBlank(iconvalues)){
					value=iconvalues;
					String[] valuearray = value.split("\\|");
					for (String v : valuearray) {
						if(StringUtils.isNotBlank(v)){
							MaterialUpload mu2 =new MaterialUpload();
							mu2.setGroupId(groupId);
							mu2.setTypeId(mu.getTypeId());
							mu2.setMemberId(memberId);
							mu2.setMd5(MD5Utils.md5Encode(v));
							//	mu2.setValue(v);
							mu2.setUrl(v);
							validMus.add(mu2);
						}
					}
				}else{
					if(StringUtils.isNotBlank(radiovalue)){
						value=radiovalue;
						mu.setValue(radiovalue);
					}
					mu.setMemberId(memberId);
					mu.setGroupId(groupId);
					mu.setMd5(MD5Utils.md5Encode(value));
					if(StringUtils.isNotBlank(value)){validMus.add(mu);}
				}
			}
         //入库操作  编辑需要删除之前的数据
			if(StringUtils.isBlank(isNew)){
                MaterialUpload qo=new MaterialUpload();
                qo.setGroupId(groupId);
                qo.setMemberId(memberId);
                //先删除之前同一用户同一组的数据
                dao.delByMemberAndGroupId(qo);
            }
			//再插入新数据
			for (MaterialUpload validMu : validMus) {
				validMu.preInsert();
				dao.insert(validMu);
			}
		return true;
	}
	@Transactional(readOnly = false)
	public boolean delMemberMaterialUpload(String memberId, String groupId) {
		if(StringUtils.isBlank(memberId)||StringUtils.isBlank(groupId)){
			return false;
		}
		MaterialUpload qo=new MaterialUpload();
		qo.setGroupId(groupId);
		qo.setMemberId(memberId);
		//入库操作
		//先删除之前同一用户同一组的数据
		dao.delByMemberAndGroupId(qo);
		return true;
	}
}